package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/5/11.
 */

import com.xianzhiOA.bean.*;
import com.xianzhiOA.service.XzEmpService;
import com.xianzhiOA.service.XzProjectService;
import com.xianzhiOA.service.XzQualityService;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.util.PageBean;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 工作质量的控制层
 *
 * @author 甘汝雷
 * @create 2017-05-11 16:25
 **/

@Controller
@RequestMapping("Quality")
@SessionAttributes("userLogin")
public class XzQualityController {
    @Autowired
    private XzQualityService qualityService;
    @Autowired
    private XzEmpService empService;
    @Autowired
    private XzProjectService projectService;
    @Autowired
    private XzWorkService workService;

    //添加
    @RequestMapping("addQuality.emp")
    public ModelAndView addQuality(XzQuality quality,@ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("redirect:selQualityAll.emp");
        quality.setQualityAudit(emp.getEmpId());
        qualityService.insert(quality);
        return mv;
    }

    //删除
    @RequestMapping("deleteQuality.emp")
    public ModelAndView deleteQuality(Long qualityId) {
        ModelAndView mv = new ModelAndView("redirect:selQualityAll.emp");
        qualityService.isdelete(qualityId);
        return mv;
    }

    //修改
    @RequestMapping("updateQuality.emp")
    public ModelAndView updateQuality(XzQuality quality) {
        ModelAndView mv = new ModelAndView("redirect:selQualityAll.emp");
        qualityService.update(quality);
        return mv;
    }

    //查询
    @RequestMapping("selQualityAll.emp")
    public ModelAndView selQualityAll(XzQuality quality, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "8")int rows, @RequestParam(defaultValue = "1")int num,
                                      Date begin, Date end, @ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/qualityShow");
        if (num==1)quality.setQualityAudit(emp.getEmpId());
        if (num==0)quality.setQualityOfficial(emp.getEmpId());
        PageBean<XzQuality> pageBean = qualityService.selAll(quality,page,rows,begin,end);
        if (!pageBean.getList().isEmpty()) {
            for (XzQuality q : pageBean.getList()) {
                Map byDate = workService.getWeekByDate(q.getQualityTime());
                q.setQualityBegin((String) byDate.get("begin"));
                q.setQualityEnd((String) byDate.get("end"));
            }
        }
        mv.addObject("num",num);
        mv.addObject("quality",quality);
        mv.addObject("qualityList",pageBean.getList());
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("rows", pageBean.getPageSize());
        mv.addObject("total", pageBean.getTotal());
        mv.addObject("begin",begin);
        mv.addObject("end",end);
        return mv;
    }

    //通过id查询
    @RequestMapping("selById.emp")
    public ModelAndView selById(Long qualityId) {
        ModelAndView mv = new ModelAndView("backEnd/qualityShow");
        XzQuality quality = qualityService.selById(qualityId);
        Map byDate = workService.getWeekByDate(quality.getQualityTime());
        quality.setQualityBegin((String) byDate.get("begin"));
        quality.setQualityEnd((String) byDate.get("end"));
        mv.addObject("q",myQuality(quality));
        mv.addObject("quality",new XzQuality());
        mv.addObject("open",1);
        return mv;
    }

    //统计缺陷清除率
    public XzQuality myQuality(XzQuality quality){
        int design=0,code=0,test=0,amount=0;
        int design1=0,code1=0,test1=0,amount1=0;
        for ( XzInject i:quality.getInjectList()) {
            design += i.getInjectDesign();
            code += i.getInjectCode();
            test += i.getInjectTest();
        }
        for (XzEliminate e:quality.getEliminateList()) {
            design1 += e.getEliminateDesign();
            code1 += e.getEliminateCode();
            test1 += e.getEliminateTest();
        }
        amount = design+code+test;
        amount1 = design1+code1+test1;
        if (design!=0&&design1!=0){
            quality.setDesign(design1*100/design);
        }
        if (code!=0&&code1!=0){
            quality.setCode(code1*100/code);
        }
        if (test!=0&&test1!=0){
            quality.setTest(test1*100/test);
        }
        if (amount!=0&&amount1!=0){
            quality.setAmount(amount1*100/amount);
        }
        return quality;
    }
    //编辑
    @RequestMapping("qualityEdit.emp")
    public ModelAndView qualityEdit(long qualityId) {
        ModelAndView mv = new ModelAndView("backEnd/qualityEdit");
        XzQuality quality = new XzQuality();
        if (qualityId!=0) {
            quality = qualityService.selById(qualityId);
        }else quality.setQualityId(0l);
        mv.addObject("quality",quality);
        mv.addObject("empList",empService.selAll(new XzEmp()));
        mv.addObject("projectList",projectService.selAll(new XzProject()));
        return mv;
    }
}